package com.tuanzi.loan.workflow.service.impl;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Sets;
import com.tuanzi.loan.workflow.entity.FormRight;
import com.tuanzi.loan.workflow.repository.FormRightRepository;
import com.tuanzi.loan.workflow.service.FormRightService;
import com.tuanzi.loan.workflow.service.WorkflowManagerService;
import com.tuanzi.loan.workflow.vo.ActivityResponse;

/**
 * @author Ray
 * @date 2018/01/16
 */
@Service
@Transactional
public class FormRightServiceImpl implements FormRightService {

	@Autowired
	private FormRightRepository formRightRepository;
	@Autowired
	private WorkflowManagerService workflowManagerService;
	// @Resource
	// private WorkflowManagerService workflowManagerService;
	//
	// @Override
	// public BaseResponse<String> saveBatch(List<FormRightSaveRequest>
	// formRightList) {
	// BaseResponse<String> response = new BaseResponse<>();
	// for (FormRightSaveRequest formRightSaveRequest : formRightList) {
	// FormRight formRightVO = new FormRight();
	// ConversionUtil.objectToObject(formRightSaveRequest, formRightVO);
	// this.save(formRightVO);
	// }
	// response.setResult(new BaseResult<>(true, "保存表单权限成功"));
	// return response;
	// }

	// @Override
	// public BaseResponse<String> deleteAndSave(FormRightDeleteAndSaveRequest
	// request) {
	// BaseResponse<String> response = new BaseResponse<>();
	// if (StringUtils.isEmpty(request.getProcDefId())) {
	// throw new
	// ServiceException(ErrorResponseEnum.REQUEST_PARAMETER_INVALID.getCode(),
	// "procDefId不能为空");
	// }
	//
	// FormRight record = new FormRight();
	// record.setProcDefId(request.getProcDefId());
	// this.delete(record);
	//
	// this.saveBatch(request.getFormRightSaveRequestList());
	// response.setResult(new BaseResult<>(true, "保存表单权限成功"));
	//
	// return response;
	// }
	//
	// @Override
	// public BaseResponse<String> saveFormRightStr(FormRightStrSaveRequest request)
	// {
	// String[] formRightArray = StringUtils.split(request.getFormRightStr(), "&");
	// List<FormRightSaveRequest> formRightSaveRequestList = new ArrayList<>();
	//
	// for (int i = 0; i < formRightArray.length; i++) {
	// JSONObject jsonObject = JSONObject.fromObject(formRightArray[i]);
	// FormRightSaveRequest formRightSaveRequest = new FormRightSaveRequest();
	// formRightSaveRequest.setProcDefId(request.getProcDefId());
	// formRightSaveRequest.setActId(jsonObject.getString("actId"));
	// formRightSaveRequest.setRightCode(jsonObject.getString("rightCode"));
	// formRightSaveRequest.setRightValue(jsonObject.getInt("rightValue"));
	// formRightSaveRequest.setSort(i + 1);
	// formRightSaveRequestList.add(formRightSaveRequest);
	// }
	//
	// FormRightDeleteAndSaveRequest deleteAndSaveRequest = new
	// FormRightDeleteAndSaveRequest();
	// deleteAndSaveRequest.setProcDefId(request.getProcDefId());
	// deleteAndSaveRequest.setFormRightSaveRequestList(formRightSaveRequestList);
	//
	// return this.deleteAndSave(deleteAndSaveRequest);
	// }

	// @Override
	// public BaseResponse<List<FormRightResponse>>
	// getFormRight(FormRightQueryRequest request) {
	// BaseResponse<List<FormRightResponse>> response = new BaseResponse<>();
	// FormRight formRightVO = new FormRight();
	// formRightVO.setProcDefId(request.getProcDefId());
	// formRightVO.setActId(request.getActivityId());
	//
	// List<FormRight> formRightVOList = this.findByParams(formRightVO);
	// List<FormRightResponse> formRightResponseList = new
	// ArrayList<>(formRightVOList.size());
	//
	// ConversionUtil.listObjectToListObject(formRightVOList, formRightResponseList,
	// FormRightResponse.class);
	//
	// response.setResult(new BaseResult<>(formRightResponseList));
	// return response;
	// }

	@Override
	public void synchronizeFormRight(String oldProcDefId, String newProcDefId) {

		List<FormRight> oldFormRights = formRightRepository.findAllByProcDefId(oldProcDefId);

		if (CollectionUtils.isNotEmpty(oldFormRights)) {

			for (FormRight oldFormRight : oldFormRights) {
				FormRight newFormRight = new FormRight();
				BeanUtils.copyProperties(oldFormRight, newFormRight, new String[] { "id" });
				newFormRight.setProcDefId(newProcDefId);
				formRightRepository.save(newFormRight);
			}

		} else {

			// 获取该流程定义下的所有活动结点
			List<ActivityResponse> activityResponses = workflowManagerService.findAllUserTaskActivity(newProcDefId);

			Set<String> rightSet = Sets.newLinkedHashSet();
			rightSet.add(FormRight.RIGHT_CODE_APPROVAL);
			rightSet.add(FormRight.RIGHT_CODE_APPROVAL_COND);
			rightSet.add(FormRight.RIGHT_CODE_REFUSAL);
			rightSet.add(FormRight.RIGHT_CODE_REJECT);
			rightSet.add(FormRight.RIGHT_CODE_TRANSFER);

			for (ActivityResponse activityResponse : activityResponses) {
				for (String rightCode : rightSet) {
					FormRight formRight = new FormRight();
					formRight.setProcDefId(newProcDefId);
					formRight.setActId(activityResponse.getActivityId());
					formRight.setRightCode(rightCode);
					formRight.setRightValue(formRight.RIGHT_VALUE_EDIT);
					formRightRepository.save(formRight);
				}
			}

		}

	}

	//
	// @Override
	// public BaseResponse<FormRightListResponse>
	// listFormRight(FormRightQueryRequest request) {
	// BaseResponse<List<FormRightResponse>> formRightResonseList =
	// this.getFormRight(request);
	//
	// // 获取该流程定义下的所有活动结点
	// AllActivityGetRequest allActivityGetRequest = new AllActivityGetRequest();
	// allActivityGetRequest.setProcDefId(request.getProcDefId());
	// BaseResponse<List<ActivityVO>> activityVOListResponse =
	// workflowManagerService.getAllActivity(allActivityGetRequest);
	// List<ActivityResponse> activityResponseList = new
	// ArrayList<>(activityVOListResponse.getResult().getData().size());
	// for (ActivityVO activityVO : activityVOListResponse.getResult().getData()) {
	// ActivityResponse activityResponse = new ActivityResponse();
	// activityResponse.setActivityId(activityVO.getActivityId());
	// activityResponse.setActivityName(activityVO.getActivityName());
	// activityResponseList.add(activityResponse);
	// }
	//
	// Set<String> rightSet = new LinkedHashSet<>();
	// rightSet.add(FormRightResponse.RIGHT_CODE_APPROVAL);
	// rightSet.add(FormRightResponse.RIGHT_CODE_APPROVAL_COND);
	// rightSet.add(FormRightResponse.RIGHT_CODE_REFUSAL);
	// rightSet.add(FormRightResponse.RIGHT_CODE_REJECT);
	// rightSet.add(FormRightResponse.RIGHT_CODE_TRANSFER);
	//
	// FormRightListResponse formRightListResponse = new FormRightListResponse();
	// formRightListResponse.setFormRightResponseList(formRightResonseList.getResult().getData());
	// formRightListResponse.setActivityResponseList(activityResponseList);
	// formRightListResponse.setRightSet(rightSet);
	//
	// return new BaseResponse<>(new BaseResult<>(formRightListResponse));
	// }
	//

	@Override
	public void deleteByProcDefId(String procDefId) {
		formRightRepository.deleteInBatch(formRightRepository.findAllByProcDefId(procDefId));
	}
}
